package com.wwp.tbshop.presenter;

import android.util.Log;

import com.wwp.tbshop.activity.ProductDetailActivity;
import com.wwp.tbshop.model.bean.BaseBean;
import com.wwp.tbshop.model.bean.CartAddBean;
import com.wwp.tbshop.model.bean.CartQueryBean;
import com.wwp.tbshop.model.bean.DeleteBean;
import com.wwp.tbshop.model.bean.UpdateBean;
import com.wwp.tbshop.view.fragment.diyview.CartFragment;

import java.util.List;

import retrofit2.Call;

/**
 * Created by windows on 2017/6/16.
 */

public class CartPresenter extends BasePresenter {

    private CartFragment mCartFragment;
    private ProductDetailActivity mProductDetailActivity;

    public CartPresenter(CartFragment cartFragment) {
        mCartFragment = cartFragment;
    }

    public CartPresenter(ProductDetailActivity productDetailActivity) {
        mProductDetailActivity = productDetailActivity;
    }

    public void addCartInfo(String userId,String productId,String productCount,String propertyId){
        Call<CartAddBean> cartAddInfo = mApiService.AddCartInfo(userId,productId,productCount,propertyId);
        cartAddInfo.enqueue(mCallback);
    }

    public void getQuryInfo(String userId){
        Call<CartQueryBean> cartInfo = mApiService.getCartInfo(userId);
        cartInfo.enqueue(mCallback);
    }



    public void updateCartInfo(String userId, String productId, String productCount,String propertyId){
        Call<UpdateBean> cartUpdateInfo = mApiService.updateCartInfo(userId, productId, productCount, propertyId);
        cartUpdateInfo.enqueue(mCallback);
    }

    public void deleteCartInfo(String userId, String productId){
        Call<DeleteBean> cartDeleteInfo = mApiService.deleteCartInfo(userId, productId);
        cartDeleteInfo.enqueue(mCallback);
    }

    @Override
    protected void onServerBug(int code) {
        Log.d("responce", "onServerBug: " + code);
    }

    @Override
    protected void onConnectError(String message) {
        Log.d("responce", "onServerBug: " + message);
    }

    @Override
    protected void onSuccess(BaseBean bean) {



        String response = bean.getResponse();
        if (response.equals("updateCart")) {
            //修改
            mCartFragment.mCartARAdapter.onUpdateResult();
        } else if (response.equals("selectCart")){
            //查询
            List<CartQueryBean.CartBean> cartQueryBeen = ((CartQueryBean) bean).getCart();
            mCartFragment.onQuerySuccess(cartQueryBeen);

            //更新购物车信息
            mCartFragment.updatCartInfo();
        }else if (response.equals("deleteCart")){
            //删除

        }
    }
}

